package com.audi.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.audi.R;
import com.audi.BaseActivity;
import com.audi.adapter.RootAccessAdapter;
import com.audi.adapter.RootAccessHomeMemberAdapter;
import com.audi.dslv.DragSortController;
import com.audi.dslv.DragSortListView;
import com.audi.dslv.DragSortListView.DropListener;
import com.audi.dto.DeviceItem;
import com.audi.dto.MemberItem;
import com.audi.dto.PlaceItem;
import com.audi.handler.MemberHandlerAction;
import com.audi.message.ActionMessage;
import com.audi.message.SendMessage;
import com.audi.utils.ProgressDialogUtil;
import com.audi.view.BasePlaceDialog;

public class RootAccessActivity extends BaseActivity implements OnClickListener {

	private DragSortListView mPlaceInRootAccessListView,
			mMemberInRootAccessListView;
	private ArrayList<PlaceItem> mPlaceRootAccessList;
	private ArrayList<MemberItem> mMemberRootAccessList;
	private RootAccessAdapter mRootAccessAdapter;
	private RootAccessHomeMemberAdapter mRootAccessHomeMemberAdapter;
	private ImageButton mIbtnMenuRootAccess;
	private TextView mBtnPlaceInfoInRootAccess, mBtnMemberInRootAccess;
	private TextView mMessageRootAccess;
	private TextView mTxtRootAccessBack;
	private LinearLayout mLnlPlaceInRootAccess, mLnlMemberInRootAccess;
	private DragSortController mControllerPlace, mControllerMember;
	public SendMessage sendMessage = new SendMessage();
	private ProgressDialog m_DlgProgress;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_root_access);
		initActionBar(R.layout.action_bar_of_root_access);
		mPlaceRootAccessList = new ArrayList<PlaceItem>();
		mMemberRootAccessList = new ArrayList<MemberItem>();
		setDisableDrawer();
		initView();
		initListener();
	}
	public DropListener onDrop = new DropListener() {
		public void drop(int from, int to) {
			if (from != to) {
             
			}
		}
	};
	public DropListener onDropPlace = new DropListener() {
		public void drop(int from, int to) {
			if (from != to) {
			    PlaceItem s = (PlaceItem) mPlaceRootAccessList.get(from);
			    mPlaceRootAccessList.remove(from);
			    mPlaceRootAccessList.add(to, s);
			    mRootAccessAdapter.notifyDataSetChanged();
			
			}

		}
	};

	@SuppressLint("ClickableViewAccessibility")
	public void initView() {
		mTxtRootAccessBack = (TextView) findViewById(R.id.title_root_access_back);
		mBtnPlaceInfoInRootAccess = (TextView) findViewById(R.id.btn_place_in_root_access);
		mBtnMemberInRootAccess = (TextView) findViewById(R.id.btn_member_in_root_access);
		mLnlPlaceInRootAccess = (LinearLayout) findViewById(R.id.lnl_place_in_root_access);
		mLnlMemberInRootAccess = (LinearLayout) findViewById(R.id.lnl_member_in_root_access);
		mPlaceInRootAccessListView = (DragSortListView) findViewById(R.id.lv_place_in_root_access);
		mMemberInRootAccessListView = (DragSortListView) findViewById(R.id.lv_member_in_root_access);
		mMessageRootAccess = (TextView) findViewById(R.id.txt_message_root_access);

		setEnaleTab(2);
		mControllerPlace = new DragSortController(mPlaceInRootAccessListView);
		mControllerPlace.setDragHandleId(R.id.rlt_drag);
		mControllerPlace.setRemoveEnabled(false);
		mControllerPlace.setSortEnabled(true);
		mControllerPlace.setDragInitMode(2);
		mPlaceInRootAccessListView.setFloatViewManager(mControllerPlace);
		mPlaceInRootAccessListView.setOnTouchListener(mControllerPlace);
		mPlaceInRootAccessListView.setDragEnabled(true);
		mPlaceInRootAccessListView.setDropListener(onDropPlace);

		mControllerMember = new DragSortController(mMemberInRootAccessListView);
		mControllerMember.setDragHandleId(R.id.rlt_drag_scene);
		mControllerMember.setRemoveEnabled(false);
		mControllerMember.setSortEnabled(true);
		mControllerMember.setDragInitMode(2);
		mMemberInRootAccessListView.setFloatViewManager(mControllerMember);
		mMemberInRootAccessListView.setOnTouchListener(mControllerMember);
		mMemberInRootAccessListView.setDragEnabled(true);
		mMemberInRootAccessListView.setDropListener(onDrop);
	}

	private void setEnaleTab(int index) {
		switch (index) {
		case 1:
			mBtnPlaceInfoInRootAccess
					.setBackgroundResource(R.drawable.btn_tab_on_new);
			mBtnMemberInRootAccess
					.setBackgroundResource(R.drawable.btn_tab_off_new);
			mMessageRootAccess
					.setText("You can only change the privacy of place.");
			mLnlPlaceInRootAccess.setVisibility(View.VISIBLE);
			mLnlMemberInRootAccess.setVisibility(View.GONE);
			mBtnPlaceInfoInRootAccess.setTextColor(Color.WHITE);
			mBtnMemberInRootAccess.setTextColor(Color.BLACK);
			CreateProgressDialog(getString(R.string.loading));
			updatePlace();
			break;
		case 2:
			mBtnPlaceInfoInRootAccess
					.setBackgroundResource(R.drawable.btn_tab_off_new);
			mBtnMemberInRootAccess
					.setBackgroundResource(R.drawable.btn_tab_on_new);
			mMessageRootAccess.setText("You can only delete member.");
			mLnlPlaceInRootAccess.setVisibility(View.GONE);
			mLnlMemberInRootAccess.setVisibility(View.VISIBLE);
			mBtnPlaceInfoInRootAccess.setTextColor(Color.BLACK);
			mBtnMemberInRootAccess.setTextColor(Color.WHITE);
			CreateProgressDialog(getString(R.string.loading));
			updateMember();
			break;
		}

	}

	private void initListener() {
		mTxtRootAccessBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(RootAccessActivity.this,
						HomeBoxSettings.class);
				startActivity(intent);
				finish();
			}
		});
		mBtnPlaceInfoInRootAccess.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				setEnaleTab(1);
			}
		});
		mBtnMemberInRootAccess.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				setEnaleTab(2);
			}
		});
		
		mPlaceInRootAccessListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                Intent intentDeviceCard = new Intent(RootAccessActivity.this,
                        NewActivityPlaceInfo.class);
                Bundle bundleInfoPlace = new Bundle();

                PlaceItem item = mPlaceRootAccessList.get(arg2);
                bundleInfoPlace.putSerializable("item", item);

                intentDeviceCard.putExtras(bundleInfoPlace);
                startActivity(intentDeviceCard);
            }
        });
	}

	@Override
	public void initActionBar(int actionBarView) {
		super.initActionBar(actionBarView);
		mIbtnMenuRootAccess = (ImageButton) this
				.findViewById(R.id.btn_menu_in_root_access);
		mIbtnMenuRootAccess.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		PopupMenu popupMenu = new PopupMenu(RootAccessActivity.this,
				mIbtnMenuRootAccess);
		popupMenu.inflate(R.menu.popup_menu_list_root_access);
		popupMenu
				.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

					@Override
					public boolean onMenuItemClick(MenuItem item) {
						switch (item.getItemId()) {
						case R.id.item_change_root_password:
							break;
						}
						return false;
					}

				});

		popupMenu.show();
	}

	public void CreateProgressDialog(String strMessage) {
		m_DlgProgress = new ProgressDialog(this);
		m_DlgProgress.setTitle(null);
		m_DlgProgress.setMessage(strMessage);
		m_DlgProgress.setIndeterminate(true);
		m_DlgProgress.setCancelable(true);
		m_DlgProgress.show();
	}

	public void DestroyProgressDialog() {
		if (m_DlgProgress != null)
			m_DlgProgress.dismiss();
	}

	@Override
	public void onRefreshData() {
	}

	public void loadListPlaceInRootAccess(
			ArrayList<PlaceItem> listPlaceDataRootAccess) {
		mPlaceRootAccessList = listPlaceDataRootAccess;
		if (mPlaceRootAccessList != null) {
			if (mRootAccessAdapter == null) {
				mRootAccessAdapter = new RootAccessAdapter(this,
						mPlaceRootAccessList);
				mPlaceInRootAccessListView.setAdapter(mRootAccessAdapter);
			} else {
				mRootAccessAdapter.setDataForAdapter(mPlaceRootAccessList);
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		updateMember();
		updatePlace();
	}

	private void updatePlace() {
		sendMessage.sendData(ActionMessage.getAllPlaceList, null,
				loadListPlaceHandlerRootAccess);
	}

	private void updateMember() {
		sendMessage.sendData(ActionMessage.getListMember, null,
				loadListMemberHandler);
	}

	public void loadListMemberInRootAccess(
			ArrayList<MemberItem> listMemberDataRootAccess) {
		mMemberRootAccessList = listMemberDataRootAccess;
		if (mMemberRootAccessList != null) {
			if (mRootAccessHomeMemberAdapter == null) {
				mRootAccessHomeMemberAdapter = new RootAccessHomeMemberAdapter(
						this, mMemberRootAccessList);
				mMemberInRootAccessListView
						.setAdapter(mRootAccessHomeMemberAdapter);
			} else {
				mRootAccessHomeMemberAdapter
						.setDataForAdapter(mMemberRootAccessList);
			}
		}
	}

	public void changeCapabilityOfPlace(PlaceItem _placeItem) {
		PlaceItem placeItem = _placeItem;
		BasePlaceDialog changeCapabilityPlace = new BasePlaceDialog(this);
		changeCapabilityPlace.changeCapabilityOfPlace(placeItem,
				handlerChangeCapabilityPlaceInRootAccess);

	}

	public void onRemovePlace(PlaceItem _placeItem) {
		PlaceItem placeItem = _placeItem;
		BasePlaceDialog removePlace = new BasePlaceDialog(this);
		removePlace.removePlace(placeItem, handlerRemovePlaceInRootAccess);
	}

	@SuppressLint("HandlerLeak")
	public Handler loadListPlaceHandlerRootAccess = new Handler() {

		public void handleMessage(final Message msg) {
			runOnUiThread(new Runnable() {
				public void run() {
					try {
						if (msg.arg1 != -1) {
							@SuppressWarnings("unchecked")
							ArrayList<PlaceItem> result = (ArrayList<PlaceItem>) msg
									.getData()
									.getSerializable(
											String.valueOf(ActionMessage.getAllPlaceList));
							loadListPlaceInRootAccess(result);
							DestroyProgressDialog();
						}
					} catch (Exception e) {
					}
				}
			});
		}
	};
	@SuppressLint("HandlerLeak")
	public Handler loadListMemberHandler = new Handler() {

		public void handleMessage(final Message msg) {
			runOnUiThread(new Runnable() {
				public void run() {
					try {
						if (msg.arg1 != -1) {
							@SuppressWarnings("unchecked")
							ArrayList<MemberItem> result = (ArrayList<MemberItem>) msg
									.getData()
									.getSerializable(
											String.valueOf(ActionMessage.getListMember));
							loadListMemberInRootAccess(result);
							DestroyProgressDialog();
							// mProgressDialog.DestroyProgressDialog();
						}
					} catch (Exception e) {
					}
				}
			});
		}
	};
	@SuppressLint("HandlerLeak")
	public Handler handlerChangeCapabilityPlaceInRootAccess = new Handler() {

		public void handleMessage(final Message msg) {
			runOnUiThread(new Runnable() {
				public void run() {
					try {
						if (msg.arg1 != -1) {
							if (msg.getData()
									.getInt(String
											.valueOf(ActionMessage.changeCapabilityPlace)) > 0) {
								CreateProgressDialog(getString(R.string.loading));
								sendMessage
										.getListPlace(loadListPlaceHandlerRootAccess);
							}
						}
					} catch (Exception e) {
					}
				}
			});
		}
	};
	@SuppressLint("HandlerLeak")
	public Handler handlerRemovePlaceInRootAccess = new Handler() {

		public void handleMessage(final Message msg) {
			runOnUiThread(new Runnable() {
				public void run() {
					try {
						if (msg.arg1 != -1) {
							if (msg.getData().getInt(
									String.valueOf(ActionMessage.deletePlace)) > 0) {
								CreateProgressDialog(getString(R.string.loading));
								sendMessage
										.getListPlace(loadListPlaceHandlerRootAccess);
							}
						}
					} catch (Exception e) {
					}
				}
			});
		}
	};

}
